package com.two.service.pinglun.controller;

import com.google.gson.Gson;
import com.two.common.entity.Pinglun;
import com.two.common.entity.User;
import com.two.common.entity.UserAndPermissions;
import com.two.common.util.CommonResult;
import com.two.service.pinglun.service.PinglunService;
import com.two.service.pinglun.vo.SayVo;
import com.two.service.pinglun.vo.SelectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pinglun/pinglun")
public class PinglunController {
    @Autowired
    private PinglunService pinglunService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //查询全部评论
    @PostMapping("/selectAll")
    public CommonResult selectAll(Integer currentPage, Integer pageSize,String token, @RequestBody SelectVo selectVo){
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Gson gson=new Gson();
        UserAndPermissions userAndPermissions = gson.fromJson(s, UserAndPermissions.class);
        User user = userAndPermissions.getUser();
        Integer rid = user.getRid();
        Integer roleid = user.getRoleid();

        return  pinglunService.findAll(currentPage,pageSize,selectVo,rid,roleid);
    }
    //删除评论
    @PostMapping("del")
    public CommonResult del(Integer id){
      return   pinglunService.del(id);
    }
    //手机端添加评论
//    @PostMapping("/addSay")
//    public CommonResult addSay(@RequestBody Pinglun pinglun){
//        return pinglunService.add(pinglun);
//    }


    //
}
